# -*- coding:utf-8 -*-
import sys
import time
import uiautomation as auto

class get_handel:

    # 获取单一窗口中的所有句柄(当前鼠标下的)
    def only_window(self,options):
        root = False,
        focus = False,
        cursor = False,
        ancestor = False,
        foreground = True,
        showAllName = False,
        depth = 0xFFFFFFFF,
        seconds = 3,
        showPid = False,
        for (o, v) in options:
            if o in ('-h', '-help'):
                sys.exit(0)
            elif o in ('-r', '-root'):
                root = True
                foreground = False
            elif o in ('-f', '-focus'):
                focus = True
                foreground = False
            elif o in ('-c', '-cursor'):
                cursor = True
                foreground = False
            elif o in ('-a', '-ancestor'):
                ancestor = True
                foreground = False
            elif o in ('-n', '-showAllName'):
                showAllName = True
            elif o in ('-p', ):
                showPid = True
            elif o in ('-d', '-depth'):
                depth = int(v)
            elif o in ('-t', '-time'):
                seconds = int(v)
        # if seconds[0] > 0:
        #     time.sleep(seconds[0])
        # auto.Logger.ColorfullyLog('Starts, Current Cursor Position: <Color=Cyan>{}</Color>'.format(auto.GetCursorPos()))
        control = None
        if root:
            control = auto.GetRootControl()
        if focus:
            control = auto.GetFocusedControl()
        if cursor:
            control = auto.ControlFromCursor()
            if depth[0] < 0:
                while depth[0] < 0 and control:
                    control = control.GetParentControl()
                    depth[0] += 1
                depth[0] = 0xFFFFFFFF
        if ancestor[0]:
            control = auto.ControlFromCursor()
            if control:
                auto.EnumAndLogControlAncestors(control, showAllName, showPid)
            else:
                auto.Logger.Write('IUIAutomation returns null element under cursor\n', auto.ConsoleColor.Yellow)
        else:
            indent = 0
            if not control:
                control = auto.GetFocusedControl()
                controlList = []
                while control:
                    controlList.insert(0, control)
                    control = control.GetParentControl()
                if len(controlList) == 1:
                    control = controlList[0]
                else:
                    control = controlList[1]
                    if foreground:
                        indent = 1
                        auto.LogControl(controlList[0], 0, showAllName, showPid)

            control_list = []
            for c, d in auto.WalkControl(control, True, 0xFFFFFFFF):
                control_ = {
                    "Depth" : str(d),
                    "ControlType" :c.ControlTypeName,
                    "ClassName" : c.ClassName,
                    "AutomationId" : c.AutomationId,
                    "Rect" : str(c.BoundingRectangle).split("(")[1].split(")")[0].split(","),
                    "Name" : c.Name
                }
                control_list.append(control_)

            return control_list;
